package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemVipTvPriceBinding
import com.starsea.book.ui.newpage.entity.VipEntity
import com.starsea.book.ui.widget.recycler.ItemTouchCallback

class VipTcAdapter(
    context: Context,
    var onChanged: ((value: VipEntity) -> Unit)? = null
) :
    RecyclerAdapter<VipEntity, ItemVipTvPriceBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemVipTvPriceBinding {
        return ItemVipTvPriceBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemVipTvPriceBinding) {
        binding.rllParent.setOnClickListener {

            getItems().forEach { it.isSelect = false }

            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it)
                it.isSelect = true
                notifyDataSetChanged()
            }

        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemVipTvPriceBinding,
        item: VipEntity,
        payloads: MutableList<Any>
    ) {
        binding.run {
            val bundle = payloads.getOrNull(0) as? Bundle
            if (bundle == null) {
//                tvDay.text =
//                    "${
//                        if (item.day < 31) "${item.day.toString()}天" else "${
//                            (item.day / 31).toInt()
//                                .toString()
//                        }月"
//                    }"
                tvDay.text = item.thaliName
                tvPrice.text = item.price ?: ""
                tvOriginPrice.text = item.gold ?: ""
//                是否热门 0:否;1:是
                binding.rllParent.isSelected = item.isSelect
//                binding.ivSel.visible(item.isSelect)

            } else {
                bundle.keySet().map {
                    when (it) {
                        "day" -> tvDay.text = "${item.day}月"
                        "price" -> tvPrice.text = item.price ?: ""
                    }
                }
            }
        }
    }


}